package com.cadre.common.utils.oldutil;

import cn.hutool.core.util.RandomUtil;
import com.cadre.common.exception.CadreException;
import com.cadre.common.utils.common.DateUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.w3c.dom.Node;

import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialBlob;
import java.io.*;
import java.math.BigInteger;
import java.sql.Blob;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/* loaded from: WordUtils.class */
public class WordUtils {
    @Autowired
    ReportConfig reportConfig;
    private static final String fileName = "干部任免审批表";

    public static void downloadWord(List<Map<String, Object>> mapList, String templatePath, HttpServletResponse response) {
        String data = new SimpleDateFormat("yyyy.MM.dd").format(new Date());
        try {
            for (Map<String, Object> map : mapList) {
                ClassPathResource classPathResource = new ClassPathResource(templatePath);
                XWPFDocument document = new XWPFDocument(classPathResource.getInputStream());
                String str = map.get("a01_a0814") == null ? "" : map.get("a01_a0814").toString().trim();
                if (ismergeCell(str)) {
                    mergeCell(document, str, 4, 4);
                } else {
                    String[] school = splitstr(str);
                    if (school.length > 0) {
                        map.put("a01_a0401", school[0]);
                        map.put("a01_a0402", school[1]);
                    }
                }
                String strcel = map.get("a01_a0903") == null ? "" : map.get("a01_a0903").toString().trim();
                if (ismergeCell(strcel)) {
                    mergeCell(document, strcel, 6, 4);
                } else {
                    String[] school2 = splitstr(strcel);
                    map.put("a01_a0501", school2[0]);
                    map.put("a01_a0502", school2[1]);
                }
                replaceTable(document, map);
                toDownload(document, classPathResource.getFilename().substring(0, classPathResource.getFilename().indexOf(".")) + "[" + data + "]_" + map.get("a01_a0101").toString() + ".docx", response);
            }
        } catch (IOException e) {
            throw new CadreException(ResultEnum.OFFICE_ERROR);
        }
    }

    public static void downloadWord(List<Map<String, Object>> mapList, String templatePath, String outPath, HttpServletResponse response) {
        String data = new SimpleDateFormat("yyyy.MM.dd").format(new Date());
        String path = outPath + RandomUtil.getRandom() + "/";
        File folder = new File(path);
        List<String> files = new ArrayList<>();
        if (!folder.exists()) {
            folder.mkdirs();
        }
        try {
            try {
                FileOutputStream zipFile = new FileOutputStream(path + fileName + "[" + data + "].zip");
                List<File> list = new ArrayList<>();
                for (Map<String, Object> map : mapList) {
                    File file = new File(path + fileName + "[" + data + "]_" + map.get("a01_a0101").toString() + ".docx");
                    XWPFDocument document = new XWPFDocument(new ClassPathResource(templatePath).getInputStream());
                    OutputStream out = new FileOutputStream(file);
                    replaceTable(document, map);
                    replaceParagraph(document, map);
                    document.write(out);
                    out.flush();
                    out.close();
                    list.add(file);
                    files.add(file.getAbsolutePath());
                }
                ZipOutputStream zipOutputStream = new ZipOutputStream(zipFile);
                zipFileAll(list, zipOutputStream);
                zipOutputStream.close();
                zipFile.close();
                toDownload(new File(path + fileName + "[" + data + "].zip"), response);
                deleteDir(path);
            } catch (IOException e) {
                throw new CadreException(ResultEnum.OFFICE_ERROR);
            }
        } catch (Throwable th) {
            deleteDir(path);
            throw th;
        }
    }

    public static void mergeWord(List<Map<String, Object>> mapList, ReportConfig reportConfig, HttpServletResponse response) {
        String data = new SimpleDateFormat("yyyy.MM.dd").format(new Date());
        String personName = "";
        String relativePath = WordUtils.class.getClassLoader().getResource("tempfile/temp.txt").getPath();
        String path = relativePath.substring(0, relativePath.lastIndexOf("/")) + "/" + DateUtils.format(new Date()) + "/" + RandomUtil.getRandom() + "/";
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        new ArrayList();
        try {
            try {
                for (Map<String, Object> map : mapList) {
                    personName = map.get("a01_a0101").toString();
                    File file = new File(path + fileName + "[" + data + "]_" + personName + ".docx");
                    System.out.println(path + fileName + "[" + data + "]_" + personName + ".docx");
                    XWPFDocument document = new XWPFDocument(new ClassPathResource(reportConfig.getWorldTmp()).getInputStream());
                    String str = map.get("a01_a0814") == null ? "" : map.get("a01_a0814").toString().trim();
                    if (ismergeCell(str)) {
                        mergeCell(document, str, 4, 4);
                    } else {
                        String[] school = splitstr(str);
                        if (school.length > 0) {
                            map.put("a01_a0401", school[0]);
                            map.put("a01_a0402", school[1]);
                        }
                    }
                    String strcel = map.get("a01_a0903") == null ? "" : map.get("a01_a0903").toString().trim();
                    if (ismergeCell(strcel)) {
                        mergeCell(document, strcel, 6, 4);
                    } else {
                        String[] school2 = splitstr(strcel);
                        map.put("a01_a0501", school2[0]);
                        map.put("a01_a0502", school2[1]);
                    }
                    OutputStream out = new FileOutputStream(file);
                    replaceTable(document, map);
                    replaceParagraph(document, map);
                    document.write(out);
                    out.flush();
                    out.close();
                    MergePDF.wordToPdf(file.getAbsolutePath(), file.getParent() + "/" + personName + ".pdf");
                }
                showPdf(response, path + personName + ".pdf");
                deleteDir(path);
            } catch (Exception e) {
                e.printStackTrace();
                throw new CadreException(ResultEnum.OFFICE_ERROR);
            }
        } catch (Throwable th) {
            deleteDir(path);
            throw th;
        }
    }

    public static void replaceTable(XWPFDocument document, Map<String, Object> map) {
        for (XWPFTable table : document.getTables()) {
            for (XWPFTableRow row : table.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    getBookmarksForCell(cell, map);
                }
            }
        }
    }

    public static void replaceParagraph(XWPFDocument document, Map<String, Object> map) {
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            List<CTBookmark> bookmarkList = paragraph.getCTP().getBookmarkStartList();
            compareBookmarks(paragraph, bookmarkList, map);
            for (int i = bookmarkList.size() - 1; i >= 0; i--) {
                Node bookEndNode = bookmarkList.get(i).getDomNode();
                bookEndNode.getParentNode().removeChild(bookEndNode);
            }
        }
    }

    private static void getBookmarksForCell(XWPFTableCell cell, Map<String, Object> map) {
        for (XWPFParagraph paragraph : cell.getParagraphs()) {
            List<CTBookmark> bookmarkList = paragraph.getCTP().getBookmarkStartList();
            compareBookmarks(paragraph, bookmarkList, map);
            for (int i = bookmarkList.size() - 1; i >= 0; i--) {
                Node bookEndNode = bookmarkList.get(i).getDomNode();
                bookEndNode.getParentNode().removeChild(bookEndNode);
            }
        }
    }

    private static void compareBookmarks(XWPFParagraph paragraph, List<CTBookmark> bookmarks, Map<String, Object> map) {
        for (CTBookmark bookMark : bookmarks) {
            if (map.get(bookMark.getName()) != null) {
                String bookValue = map.get(bookMark.getName()).toString().replaceAll("\t", "  ");
                XWPFRun run = paragraph.createRun();
                if (bookMark.getName().equalsIgnoreCase("a01_a1701")) {
                    JianLiData jianLiData = new JianLiFormat().processJianLi(bookValue);
                    run.setFontSize(jianLiData.getFont());
                    lineFeed(run, jianLiData.getJianli());
                    String space = jianLiData.getFontSapce();
                    if (space.indexOf(".") > -1) {
                        space = space.substring(0, space.length() - 2);
                    }
                    CTPPr pPPr = getParagraphCTPPr(paragraph);
                    CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing() : pPPr.addNewSpacing();
                    pSpacing.setLineRule(STLineSpacingRule.EXACT);
                    pSpacing.setLine(new BigInteger(space));
                } else if (bookMark.getName().equals("a01_a0101") || bookMark.getName().equals("a01_a0127")) {
                    String value = map.get(bookMark.getName()).toString();
                    run.setFontSize(executeFontSize(value, initJiankangXingmConfig()));
                    run.setText(value);
                } else {
                    if (bookMark.getName().equalsIgnoreCase("a01_age")) {
                        try {
                            ((XWPFRun) paragraph.getRuns().get(0)).setText(bookValue, -2);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (bookMark.getName().equals("a0171_a")) {
                        try {
                            byte[] a0171A = (byte[]) map.get("a0171_a");
                            Blob blob = new SerialBlob(a0171A);
                            run.addPicture(blob.getBinaryStream(), 5, "pic", Units.toEMU(103.0d), Units.toEMU(140.0d));
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    } else if (bookMark.getName().equals("a01_a0184a")) {
                        String value2 = map.get(bookMark.getName()).toString().trim();
                        System.out.println("a01_a0184a=====" + value2 + ",run fontsize:" + run.getFontSize());
                        int snum = 1;
                        try {
                            snum = value2.split(";").length;
                        } catch (Exception e3) {
                        }
                        System.out.println("snum=====" + snum);
                        if (snum >= 3) {
                            run.setFontSize(8);
                        }
                        if (value2.contains("\n")) {
                            run.setFontSize(8);
                            lineFeed(run, bookValue);
                        } else {
                            if (value2.length() > 5 && !StrValidate.hasDigit(value2)) {
                                run.setFontSize(10);
                            }
                            run.setText(value2);
                        }
                    } else if (bookMark.getName().equals("quanrizhi") || bookMark.getName().equals("zaizi")) {
                        String value3 = map.get(bookMark.getName()).toString().trim();
                        if (ismergeCell(value3)) {
                            run.setFontSize(executeFontSize(value3, initxuelixueweiConfig()));
                            run.setText(value3);
                        } else {
                            String[] values = splitstr(value3);
                            Map<Integer, Integer> tmap = initxuelixueweiSigleConfig();
                            run.setFontSize(executeFontSize(values[0], tmap));
                            run.setText(values[0]);
                            run.addBreak();
                            int fontsize2 = executeFontSize(values[1], tmap);
                            XWPFRun run1 = paragraph.createRun();
                            run1.setFontSize(fontsize2);
                            run1.setText(values[1]);
                        }
                    } else if (bookMark.getName().equals("a01_a0324")) {
                        String value4 = map.get(bookMark.getName()).toString();
                        run.setFontSize(executeFontSize(value4, initJiszwConfig()));
                        run.setText(value4);
                    } else if (bookMark.getName().equals("a01_a0187")) {
                        String value5 = map.get(bookMark.getName()).toString();
                        run.setFontSize(executeFontSize(value5, initZhuancConfig()));
                        run.setText(value5);
                    } else if (bookMark.getName().equals("a01_a0401")) {
                        String value6 = map.get(bookMark.getName()).toString();
                        int fontsize = executeFontSize(value6, initBiyeyuanxiaosignlConfig());
                        run.setFontSize(fontsize);
                        run.setText(value6);
                        if (fontsize == 8) {
                            CTPPr pPPr2 = getParagraphCTPPr(paragraph);
                            CTSpacing pSpacing2 = pPPr2.getSpacing() != null ? pPPr2.getSpacing() : pPPr2.addNewSpacing();
                            pSpacing2.setLineRule(STLineSpacingRule.EXACT);
                            pSpacing2.setLine(new BigInteger("160"));
                        }
                    } else if (bookMark.getName().equals("a01_a0402")) {
                        String value7 = map.get(bookMark.getName()).toString();
                        int fontsize3 = executeFontSize(value7, initBiyeyuanxiaosignlConfig());
                        run.setFontSize(fontsize3);
                        run.setText(value7);
                        if (fontsize3 == 8) {
                            CTPPr pPPr3 = getParagraphCTPPr(paragraph);
                            CTSpacing pSpacing3 = pPPr3.getSpacing() != null ? pPPr3.getSpacing() : pPPr3.addNewSpacing();
                            pSpacing3.setLineRule(STLineSpacingRule.EXACT);
                            pSpacing3.setLine(new BigInteger("160"));
                        }
                    } else if (bookMark.getName().equals("a01_a0501")) {
                        String value8 = map.get(bookMark.getName()).toString();
                        int fontsize4 = executeFontSize(value8, initBiyeyuanxiaosignlConfig());
                        run.setFontSize(fontsize4);
                        run.setText(value8);
                        if (fontsize4 == 8) {
                            CTPPr pPPr4 = getParagraphCTPPr(paragraph);
                            CTSpacing pSpacing4 = pPPr4.getSpacing() != null ? pPPr4.getSpacing() : pPPr4.addNewSpacing();
                            pSpacing4.setLineRule(STLineSpacingRule.EXACT);
                            pSpacing4.setLine(new BigInteger("160"));
                        }
                    } else if (bookMark.getName().equals("a01_a0502")) {
                        String value9 = map.get(bookMark.getName()).toString();
                        int fontsize5 = executeFontSize(value9, initBiyeyuanxiaosignlConfig());
                        run.setFontSize(fontsize5);
                        run.setText(value9);
                        if (fontsize5 == 8) {
                            CTPPr pPPr5 = getParagraphCTPPr(paragraph);
                            CTSpacing pSpacing5 = pPPr5.getSpacing() != null ? pPPr5.getSpacing() : pPPr5.addNewSpacing();
                            pSpacing5.setLineRule(STLineSpacingRule.EXACT);
                            pSpacing5.setLine(new BigInteger("160"));
                        }
                    } else if (bookMark.getName().equals("new1002") || bookMark.getName().equals("a01_a0277") || bookMark.getName().equals("a01_a0281")) {
                        String value10 = map.get(bookMark.getName()).toString();
                        run.setFontSize(executeFontSize(value10, initXianrenzhiwuConfig()));
                        run.setText(value10);
                    } else if (bookMark.getName().equals("a01_a1421")) {
                        String value11 = map.get(bookMark.getName()).toString();
                        run.setFontSize(executeFontSize(value11, initJiangChengConfig()));
                        lineFeed(run, value11);
                    } else if (bookMark.getName().equals("a01_a1511")) {
                        String value12 = map.get(bookMark.getName()).toString();
                        run.setFontSize(executeFontSize(value12, initKaoHConfig()));
                        lineFeed(run, value12);
                    } else if (bookMark.getName().startsWith("new201")) {
                        String value13 = map.get(bookMark.getName()).toString();
                        run.setFontSize(executeFontSize(value13, initJiatingNameConfig()));
                        run.setText(value13);
                    } else if (bookMark.getName().startsWith("new203")) {
                        String value14 = map.get(bookMark.getName()).toString();
                        run.setFontSize(executeFontSize(value14, initJIASHUZHIWUConfig()));
                        run.setText(value14);
                    } else {
                        lineFeed(run, bookValue);
                    }
                }
            }
        }
    }

    private static String fontZoomer(XWPFRun run, String bookValue) {
        int[] lengthrules = {60, 70, 76, 85, 95, 107, 122, 143, 150};
        int[] iArr = {19, 22, 24, 26, 29, 33, 38, 44, 49};
        int[] lineRulesNew = {23, 26, 27, 30, 36, 38, 43, 49, 55};
        int[] fontsizes = {14, 12, 11, 10, 9, 8, 7, 6, 5};
        String[] strs = {""};
        if (bookValue.indexOf("\r\n") > -1) {
            strs = bookValue.split("\r\n");
        } else if (bookValue.indexOf("\r") > -1) {
            strs = bookValue.split("\r");
        } else if (bookValue.indexOf("\n") > -1) {
            strs = bookValue.split("\n");
        }
        List<Integer> lengths = new ArrayList<>();
        for (String str : strs) {
            lengths.add(Integer.valueOf(getWordCount(str)));
        }
        int maxlength = ((Integer) Collections.max(lengths)).intValue();
        int maxline = strs.length;
        int fontsizeFlength = 6;
        int fontsizeFline = 6;
        int i = 0;
        while (true) {
            if (i >= lengthrules.length) {
                break;
            } else if (maxlength <= lengthrules[i]) {
                fontsizeFlength = fontsizes[i];
                System.out.println("根据每行字数设置的字体大小为：" + fontsizeFlength);
                break;
            } else {
                i++;
            }
        }
        int i2 = 0;
        while (true) {
            if (i2 >= lineRulesNew.length) {
                break;
            } else if (maxline <= lineRulesNew[i2]) {
                fontsizeFline = fontsizes[i2];
                System.out.println("根据行数设置的字体大小为：" + fontsizeFline);
                break;
            } else {
                i2++;
            }
        }
        int fontsize = fontsizeFlength >= fontsizeFline ? fontsizeFline : fontsizeFlength;
        run.setFontSize(fontsize);
        return initFontSpaceMap().get(Integer.valueOf(fontsize));
    }

    private static void lineBreak(XWPFParagraph para, XWPFRun run, String bookValue) {
        para.removeRun(0);
        if (bookValue.indexOf("\r\n") > -1) {
            String[] strs = bookValue.split("\r\n");
            for (int i = 0; i < strs.length; i++) {
                para.insertNewRun(i).setText(strs[i]);
                if (i + 1 < strs.length) {
                    para.insertNewRun(i).addCarriageReturn();
                }
            }
        } else if (bookValue.indexOf("\r") > -1) {
            String[] strs2 = bookValue.split("\r");
            for (int i2 = 0; i2 < strs2.length; i2++) {
                para.insertNewRun(i2).setText(strs2[i2]);
                if (i2 + 1 < strs2.length) {
                    para.insertNewRun(i2).addCarriageReturn();
                }
            }
        } else if (bookValue.indexOf("\n") > -1) {
            String[] strs3 = bookValue.split("\n");
            for (int i3 = 0; i3 < strs3.length; i3++) {
                para.insertNewRun(i3).setText(strs3[i3]);
                if (i3 + 1 < strs3.length) {
                    para.insertNewRun(i3).addCarriageReturn();
                }
            }
        } else {
            run.setText(bookValue);
        }
    }

    private static void lineFeed(XWPFRun run, String bookValue) {
        if (bookValue.indexOf("\r\n") > -1) {
            String[] strs = bookValue.split("\r\n");
            for (int i = 0; i < strs.length; i++) {
                run.setText(strs[i]);
                if (i + 1 < strs.length) {
                    run.addBreak();
                }
            }
        } else if (bookValue.indexOf("\r") > -1) {
            String[] strs2 = bookValue.split("\r");
            for (int i2 = 0; i2 < strs2.length; i2++) {
                run.setText(strs2[i2]);
                if (i2 + 1 < strs2.length) {
                    run.addBreak();
                }
            }
        } else if (bookValue.indexOf("\n") > -1) {
            String[] strs3 = bookValue.split("\n");
            for (int i3 = 0; i3 < strs3.length; i3++) {
                run.setText(strs3[i3]);
                if (i3 + 1 < strs3.length) {
                    run.addBreak();
                }
            }
        } else {
            run.setText(bookValue);
        }
    }

    private static int url_type(String url) {
        String type = url.substring(url.indexOf(".") + 1);
        char c = 65535;
        switch (type.hashCode()) {
            case 100542:
                if (type.equals("emf")) {
                    c = 0;
                    break;
                }
                break;
            case 105441:
                if (type.equals("jpg")) {
                    c = 3;
                    break;
                }
                break;
            case 111145:
                if (type.equals("png")) {
                    c = 4;
                    break;
                }
                break;
            case 117840:
                if (type.equals("wmf")) {
                    c = 1;
                    break;
                }
                break;
            case 3440682:
                if (type.equals("pict")) {
                    c = 2;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                return 2;
            case 1:
                return 3;
            case DateUtil.SUB_MONTH:
                return 4;
            case 3:
                return 5;
            case 4:
                return 6;
            default:
                return 5;
        }
    }

    private static String url_name(String url) {
        return url.substring(url.lastIndexOf("/") + 1, url.indexOf("."));
    }

    private static List<Integer> strIndex(String str, String key) {
        List<Integer> list = new ArrayList<>();
        int startindex = 0;
        int count = 0;
        while (true) {
            int startindex2 = str.indexOf(key, startindex);
            if (startindex2 < 0) {
                return list;
            }
            count++;
            list.add(Integer.valueOf(startindex2));
            startindex = startindex2 + key.length();
        }
    }

    private static List<String> toFormat(String str, String key) {
        List<Integer> indexs = strIndex(str, key);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < indexs.size(); i++) {
            if (str.substring(indexs.get(i).intValue() + key.length(), indexs.get(i).intValue() + 7).indexOf(".") <= 0) {
                list.add(str.substring(indexs.get(i).intValue() - 7, indexs.get(i).intValue() + key.length()) + "       " + str.substring(indexs.get(i).intValue() + key.length()));
            } else if (i < indexs.size() - 1) {
                list.add(str.substring(indexs.get(i).intValue() - 7, indexs.get(i).intValue() + key.length() + 7) + str.substring(indexs.get(i).intValue() + key.length() + 7, indexs.get(i + 1).intValue() - 7));
            } else {
                list.add(str.substring(indexs.get(i).intValue() - 7, indexs.get(i).intValue() + key.length() + 7) + str.substring(indexs.get(i).intValue() + key.length() + 7));
            }
        }
        return list;
    }

    private static void zipFileAll(List files, ZipOutputStream outputStream) {
        Iterator it = files.iterator();
        while (it.hasNext()) {
            zipFile((File) it.next(), outputStream);
        }
    }

    private static void zipFile(File file, ZipOutputStream outputStream) {
        try {
            if (file.exists()) {
                if (file.isFile()) {
                    FileInputStream IN = new FileInputStream(file);
                    BufferedInputStream bins = new BufferedInputStream(IN, 1024);
                    outputStream.putNextEntry(new ZipEntry(file.getName()));
                    byte[] buffer = new byte[1024];
                    while (true) {
                        int nNumber = bins.read(buffer);
                        if (nNumber == -1) {
                            break;
                        }
                        outputStream.write(buffer, 0, nNumber);
                    }
                    outputStream.flush();
                    bins.close();
                    IN.close();
                } else {
                    for (File file1 : file.listFiles()) {
                        zipFile(file1, outputStream);
                    }
                }
            }
        } catch (Exception e) {
            throw new CadreException(ResultEnum.OFFICE_ERROR);
        }
    }

    public static boolean deleteFiles(List<String> files) {
        for (int i = 0; i < files.size(); i++) {
            new File(files.get(i)).delete();
        }
        return true;
    }

    public static boolean deleteDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return false;
        }
        for (String name : file.list()) {
            File temp = new File(path, name);
            if (temp.isDirectory()) {
                deleteDir(temp.getAbsolutePath());
                temp.delete();
            } else if (!temp.delete()) {
            }
        }
        return true;
    }

    public static void toDownloadLrMx(File file, HttpServletResponse response) {
        try {
            try {
                InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                String fileName2 = file.getName();
                OutputStream out = new BufferedOutputStream(response.getOutputStream());
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/x-msdownload");
                response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName2.getBytes("UTF-8"), "ISO8859-1") + "\"");
                out.write(buffer);
                out.flush();
                out.close();
                try {
                    deleteDir(file.getParent());
                } catch (Exception e) {
                    throw new CadreException(ResultEnum.OFFICE_ERROR);
                }
            } catch (IOException e2) {
                throw new CadreException(ResultEnum.OFFICE_ERROR);
            }
        } catch (Throwable th) {
            try {
                deleteDir(file.getParent());
                throw th;
            } catch (Exception e3) {
                throw new CadreException(ResultEnum.OFFICE_ERROR);
            }
        }
    }

    public static void toDownload(File file, HttpServletResponse response) {
        try {
            try {
                InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                String fileName2 = file.getName();
                OutputStream out = new BufferedOutputStream(response.getOutputStream());
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/x-msdownload");
                response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName2.getBytes("UTF-8"), "ISO8859-1") + "\"");
                out.write(buffer);
                out.flush();
                out.close();
                try {
                    deleteDir(file.getParent());
                } catch (Exception e) {
                    throw new CadreException(ResultEnum.OFFICE_ERROR);
                }
            } catch (IOException e2) {
                throw new CadreException(ResultEnum.OFFICE_ERROR);
            }
        } catch (Throwable th) {
            try {
                deleteDir(file.getParent());
                throw th;
            } catch (Exception e3) {
                throw new CadreException(ResultEnum.OFFICE_ERROR);
            }
        }
    }

    public static void toDownload(XWPFDocument document, String fileName2, HttpServletResponse response) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName2.getBytes("UTF-8"), "ISO8859-1") + "\"");
            OutputStream out = response.getOutputStream();
            document.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            throw new CadreException(ResultEnum.OFFICE_ERROR);
        }
    }

    public static void showPdf(HttpServletResponse response, String fileName2) throws IOException {
        response.setStatus(200);
        response.setContentType("application/pdf;charset=UTF-8");
        FileInputStream in = new FileInputStream(new File(fileName2));
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[1024];
        while (in.read(b) != -1) {
            out.write(b);
        }
        out.flush();
        in.close();
        out.close();
    }

    public static int getWordCount(String s) {
        int length = 0;
        for (int i = 0; i < s.length(); i++) {
            int ascii = Character.codePointAt(s, i);
            if (ascii < 0 || ascii > 255) {
                length += 2;
            } else {
                length++;
            }
        }
        return length;
    }

    private static Map<Integer, Integer> initJiangChengConfig() {
        Map<Integer, Integer> a01_a1421 = new LinkedHashMap<>();
        a01_a1421.put(242, 14);
        a01_a1421.put(284, 12);
        a01_a1421.put(426, 10);
        a01_a1421.put(620, 8);
        return a01_a1421;
    }

    private static Map<Integer, Integer> initJIASHUZHIWUConfig() {
        Map<Integer, Integer> a01_a1421 = new LinkedHashMap<>();
        a01_a1421.put(62, 14);
        a01_a1421.put(74, 12);
        a01_a1421.put(90, 10);
        a01_a1421.put(170, 8);
        return a01_a1421;
    }

    private static Map<Integer, Integer> initBiyeyuanxiaoConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(45, 14);
        map.put(54, 12);
        map.put(70, 10);
        map.put(148, 8);
        return map;
    }

    private static Map<Integer, Integer> initBiyeyuanxiaosignlConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(22, 14);
        map.put(26, 12);
        map.put(32, 10);
        map.put(80, 8);
        return map;
    }

    private static Map<Integer, Integer> initXianrenzhiwuConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(98, 14);
        map.put(114, 12);
        map.put(152, 10);
        map.put(262, 8);
        return map;
    }

    private static Map<Integer, Integer> initJiankangXingmConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(8, 14);
        map.put(18, 12);
        map.put(22, 10);
        map.put(40, 8);
        return map;
    }

    private static Map<Integer, Integer> initZhuancConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(34, 14);
        map.put(38, 12);
        map.put(50, 10);
        map.put(92, 8);
        return map;
    }

    private static Map<Integer, Integer> initJiszwConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(32, 14);
        map.put(34, 12);
        map.put(40, 10);
        map.put(80, 8);
        return map;
    }

    private static Map<Integer, Integer> initKaoHConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(118, 14);
        map.put(212, 12);
        map.put(338, 10);
        map.put(446, 8);
        return map;
    }

    private static Map<Integer, Integer> initJiatingNameConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(14, 14);
        map.put(18, 12);
        map.put(22, 10);
        map.put(40, 8);
        return map;
    }

    private static Map<Integer, Integer> initxuelixueweiConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(32, 14);
        map.put(36, 12);
        map.put(42, 10);
        map.put(52, 8);
        return map;
    }

    private static Map<Integer, Integer> initxuelixueweiSigleConfig() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(16, 14);
        map.put(18, 12);
        map.put(20, 11);
        map.put(22, 10);
        map.put(24, 9);
        map.put(28, 8);
        return map;
    }

    private static Map<Integer, String> initFontSpaceMap() {
        Map<Integer, String> map = new LinkedHashMap<>();
        map.put(14, "300");
        map.put(12, "270.0");
        map.put(11, "260");
        map.put(10, "230.0");
        map.put(9, "190.0");
        map.put(8, "180");
        map.put(7, "160");
        map.put(6, "140");
        map.put(5, "120");
        return map;
    }

    private static int executeFontSize(String value, Map<Integer, Integer> map) {
        int fontSize = 6;
        int length = getWordCount(value);
        Iterator<Integer> it = map.keySet().iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Integer key = it.next();
            if (key.intValue() >= length) {
                fontSize = map.get(key).intValue();
                break;
            }
        }
        return fontSize;
    }

    public static void mergeCell(XWPFDocument document, String value, int row, int column) {
        XWPFTable mainTable = document.getTables().get(1);
        XWPFTableCell cell = mainTable.getRow(row).getCell(column);
        XWPFTableCell cell1 = mainTable.getRow(row + 1).getCell(column);
        cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
        cell1.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
        cell1.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        initBiyeyuanxiaoConfig();
        int fontsize = executeFontSize(value, initBiyeyuanxiaoConfig());
        XWPFRun run = ((XWPFParagraph) cell.getParagraphs().get(0)).createRun();
        run.setFontSize(fontsize);
        run.setText(value);
    }

    public static boolean ismergeCell(String value) {
        boolean flag;
        if (value == null || value.equals("")) {
            return false;
        }
        if (value.indexOf("\r\n") > -1 || value.indexOf("\r") > -1 || value.indexOf("\n") > -1) {
            flag = false;
        } else {
            flag = true;
        }
        return flag;
    }

    public static String[] splitstr(String value) {
        String[] strs = {"", ""};
        if (value.indexOf("\r\n") > -1) {
            strs = value.split("\r\n");
        } else if (value.indexOf("\r") > -1) {
            strs = value.split("\r");
        } else if (value.indexOf("\n") > -1) {
            strs = value.split("\n");
        }
        return strs;
    }

    public static CTPPr getParagraphCTPPr(XWPFParagraph paragraph) {
        CTPPr pPPr = null;
        if (paragraph.getCTP() != null) {
            if (paragraph.getCTP().getPPr() != null) {
                pPPr = paragraph.getCTP().getPPr();
            } else {
                pPPr = paragraph.getCTP().addNewPPr();
            }
        }
        return pPPr;
    }
}
